import simpy
import random
import time

class Airplane:
    """ 航班类
    """
    def __init__(
            self, env, id, 
            airline_df, 
            land_at_airport
            ):
        
        self.env = env

        self.id = id

        self.name = airline_df['name']
        self.from_city = airline_df['from_city']
        self.from_airport = airline_df['from_airport']
        self.flight_miles = airline_df['flight_miles']
        self.aircraft_model = airline_df['aircraft_model']
        self.airline_company = airline_df['airline_company']

        self.arrival_time = airline_df[['arrival_time']].values
        self.takeoff_time = None

        self.time_for_landing = max(
            1 / 60,
            # 通常情况下，
            # 起飞航空器从等待位置到对正跑道时间应控制在 60s 以内；
            # 着陆航空器从接地到滑出跑道应控制在 50s 以内；
            # 如需更长时间占用跑道，应尽早通知管制员
            # 这里暂时取均值 25
            random.expovariate(
                1 / (
                    (25 / 60) * 1 # 换算为分钟
                )
            )
        )

        self.land_at_airport = land_at_airport

        self.is_left = False

    def process(self):
        self.env.process(self.landing())

    def landing(self):
        print(
            self.name, 
            "is ready to land at", 
            time.strftime(
                  "%H:%M:%S", 
                  time.gmtime(self.env.now)
                )
            )

        with self.land_at_airport.runways.request() as req:
            yield req
            # yield self.env.timeout(1 / 25)
            yield self.env.timeout(self.time_for_landing)
            print(
                self.name, 
                "landed at time", 
                time.strftime(
                  "%H:%M:%S", 
                  time.gmtime(self.env.now)
                  )
                )

            self.land_at_airport.taxi_way_system.count_airplane += 1
            self.land_at_airport.parking_bays.put(1)
            self.env.process(
                self.ground_service()
            )

    def ground_service(self):
        with self.land_at_airport.ground_services_groups.request(
            ) as req:
            yield req
            yield self.env.timeout(
                # 按照《机场时刻容量评估技术规范》：
                # “空管单位提供统计数据，
                # 如没有统计数据，则由评
                # 估单位现场采集。”
                # 我们什么也没有，所以估值 120
                max(
                    1/60,
                    random.expovariate(1 / 120)
                )
            )
            self.land_at_airport.taxi_way_system.count_airplane += 1
            self.env.process(self.take_off())

    def take_off(self):
        print(
            self.name, 
            "is ready to take off at time", 
            time.strftime(
                  "%H:%M:%S", 
                  time.gmtime(self.env.now)
                )
            )

        with self.land_at_airport.runways.request() as req:
            yield req
            yield self.env.timeout(
                # 起飞航空器从等待位置到对正跑道时间应控制在 60s 以内；
                # 暂时取均值 30s 
                max(
                    1 / 60, 
                    random.expovariate(1 / 0.5)
                )
            )
            self.takeoff_time = self.env.now
            self.is_left = True

            print(
                self.name, 
                "took off at time", 
                time.strftime(
                  "%H:%M:%S", 
                  time.gmtime(self.env.now)
                  )
            )
    

    